<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-auth="'coupons.create'" @click="create"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" v-auth="'coupons.delete'" v-if="selection.length>0" :disabled="selection.length==0" @click="dels"></el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search" placeholder="搜索" @clear="this.$TABLE.searchSubmit(this)" clearable></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="this.$TABLE.searchSubmit(this)"></el-button>
                    <imiFilterBar :options="options" @filterChange="filterChange"></imiFilterBar>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" :params="params" @selection-change="selectionChange" @cell-dblclick="doubleClick" stripe remoteSort remoteFilter>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="id" prop="id" >
                                    
				</el-table-column>
                <el-table-column label="名称" prop="name" >
                                    
				</el-table-column>
                <el-table-column label="类型" prop="type" >
                                    
				</el-table-column>
                <el-table-column label="类型" prop="type_status" >
                                    
				</el-table-column>
                <el-table-column label="状态" prop="status" >
                                    
				</el-table-column>
                <el-table-column label="适用商品" prop="goods_ids" >
                                    
				</el-table-column>
                <el-table-column label="券面额" prop="jian" >
                                    
				</el-table-column>
                <el-table-column label="每次可抵扣(次卡)" prop="deduct" >
                                    
				</el-table-column>
                <el-table-column label="返回贝壳" prop="return_shell" >
                                    
				</el-table-column>
                <el-table-column label="消费门槛" prop="man" >
                                    
				</el-table-column>
                <el-table-column label="库存" prop="stock" >
                                    
				</el-table-column>
                <el-table-column label="每人限制" prop="limit" >
                                    
				</el-table-column>
                <el-table-column label="领取周期" prop="gettime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
                <el-table-column label="有效期天" prop="day" >
                                    
				</el-table-column>
                <el-table-column label="小时" prop="hour" >
                                    
				</el-table-column>
                <el-table-column label="分钟" prop="min" >
                                    
				</el-table-column>
                <el-table-column label="复购次数" prop="repeat" >
                                    
				</el-table-column>
                <el-table-column label="描述" prop="description" >
                                    
				</el-table-column>
                <el-table-column label="创建时间" prop="createtime"  align="right" width="180" :formatter="this.$TABLE.datetime">
                                    
				</el-table-column>
                <el-table-column label="使用开始时间" prop="usetimestart" >
                                    
				</el-table-column>
                <el-table-column label="使用结束时间" prop="usetimeend" >
                                    
				</el-table-column>
                <el-table-column label="店铺ID" prop="store_id" >
                                    
				</el-table-column>
                <el-table-column label="赠送" prop="give_status" >
                                    
				</el-table-column>
                <el-table-column label="领取状态" prop="get_status" >
                                    
				</el-table-column>
                <el-table-column label="混合付" prop="mix_status" >
                                    
				</el-table-column>
                <el-table-column label="场景" prop="scene" >
                                    
				</el-table-column>
                
                <el-table-column label="操作" fixed="right" align="right" width="140">
                    <template #default="scope">
                        <el-button type="text" v-auth="'coupons.update'" size="small" @click="update(scope.row)">编辑
                        </el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="del(scope.row)">
                            <template #reference>
                                <el-button type="text" v-auth="'coupons.delete'" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>
<script>
    import saveDialog from './save';
    import imiFilterBar from '@/components/imiFilterBar';
    export default {
        name: 'coupons',
        components: {
            saveDialog,
            imiFilterBar
        },
        data() {
            return {
                options: [
                    {
                        label: 'id',
                        value: 'id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '名称',
                        value: 'name',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '类型',
                        value: 'type',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '类型',
                        value: 'type_status',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '状态',
                        value: 'status',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '适用商品',
                        value: 'goods_ids',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '券面额',
                        value: 'jian',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '每次可抵扣(次卡)',
                        value: 'deduct',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '返回贝壳',
                        value: 'return_shell',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '消费门槛',
                        value: 'man',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '库存',
                        value: 'stock',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '每人限制',
                        value: 'limit',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '领取周期',
                        value: 'gettime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '有效期天',
                        value: 'day',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '小时',
                        value: 'hour',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '分钟',
                        value: 'min',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '复购次数',
                        value: 'repeat',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '秒',
                        value: 's',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '描述',
                        value: 'description',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '创建时间',
                        value: 'createtime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '更新时间',
                        value: 'updatetime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '删除时间',
                        value: 'deletetime',
                        type: 'datetimerange',
                        operator: 'between',
                    },
					{
                        label: '使用开始时间',
                        value: 'usetimestart',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '使用结束时间',
                        value: 'usetimeend',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '店铺ID',
                        value: 'store_id',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '赠送',
                        value: 'give_status',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '领取状态',
                        value: 'get_status',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '混合付',
                        value: 'mix_status',
                        type: 'text',
                        operator: '=',
                    },
					{
                        label: '场景',
                        value: 'scene',
                        type: 'text',
                        operator: '=',
                    },
					                    
                ],
                dialog: {
                    save: false
                },
                apiObj: this.$API.coupons.read,
                selection: [],
                search: '',
                params:{},
            }
        },
        created() {
            this.$TABLE.setFilterValue(this);
        },
        mounted() {
            
        },
        methods: {
            doubleClick(row) {
                this.update(row);
            },
            filterChange(data) {
                this.$TABLE.filter(data, this);
            },
            create() {
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open()
                })
            },
            update(row) {
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('update').setData(row)
                })
            },
            async del(row) {
                var res = await this.$API.coupons.delete.post({ids: row.id});
                if (res.code == 200) {
                    this.$refs.table.refresh()
                    this.$message.success(res.message)
                } else {
                    this.$alert(res.message, "提示", {type: 'error'})
                }
            },
            async dels() {
                if (!this.selection.length) {
                    this.$message.warning("请选择要删除的数据")
                    return false;
                }
                var ids = [];
                for (const i in this.selection) {
                    ids.push(this.selection[i].id)
                }
                this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                    type: 'warning'
                }).then(() => {
                    const loading = this.$loading();
                    this.$API.coupons.delete.post({ids:ids}).then(res => {
                        if (res.code == 200) {
                            this.$refs.table.refresh()
                            this.$message.success(res.message)
                        } else {
                            this.$message.error(res.message)
                        }
                        loading.close();
                    });
                })
            },
            selectionChange(selection) {
                this.selection = selection;
            },
            handleSuccess() {
                this.$refs.table.refresh()
            }
        }
    }
</script>